package com.ruoyi.web.controller.net;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.DeviceStatusIsisNeighb;
import com.ruoyi.net.service.IDeviceStatusIsisNeighbService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 设备状态-isis邻居状态Controller
 * 
 * @author tony
 * @date 2024-12-18
 */
@Api(tags = "设备状态-isis邻居状态")
@RestController
@RequestMapping("/device/status/isis/neighb")
public class DeviceStatusIsisNeighbController extends BaseController {
    @Autowired
    private IDeviceStatusIsisNeighbService deviceStatusIsisNeighbService;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 读取设备状态-isis邻居状态列表
     */
    @ApiOperation(value = "读取设备状态-isis邻居状态列表", httpMethod = "GET")
//    @PreAuthorize("@ss.hasPermi('net:neighb:list')")
    @GetMapping("/read")
    public TableDataInfo read(DeviceStatusIsisNeighb deviceStatusIsisNeighb) {
        List<DeviceStatusIsisNeighb> deviceStatusIsisNeighbList = new ArrayList<>();
        StringBuilder xml = new StringBuilder();
        xml.append("<filter type=\"subtree\">" +
                "<isis-oper xmlns=\"http://fiberhome.com/fhnw/yang/ipprotocol/routing/fos-isis-oper\">" +
                "    <isis-instances>" +
                "        <isis-instance>");
        if (StringUtils.isNotEmpty(deviceStatusIsisNeighb.getInstanceName())) {
            xml.append("     <instance-name>")
                    .append(deviceStatusIsisNeighb.getInstanceName())
                    .append("</instance-name>");
        }
        xml.append("</isis-instance>" +
                "    </isis-instances>" +
                "</isis-oper>" +
                "</filter>");
        NetElement netElement = netElementService.getCacheObject(deviceStatusIsisNeighb.getElementId());
        if (sysConfigService.selectDebugEnabled()) {
            deviceManager.getDevice(netElement.getIp());
            String rpcReply = deviceManager.getStatus(xml.toString());
            // 解析xml
            NodeList list = findNetConfXmlNodes(rpcReply, "//isis-instances/isis-instance/neighbors/neighbor");
            if (list != null && list.getLength() > 0) {
                for (int i = 0; i < list.getLength(); i++) {
                    DeviceStatusIsisNeighb entity = new DeviceStatusIsisNeighb();
                    Node node = list.item(i);
                    // 通过标签名获取节点的值，中间系统标识
                    entity.setSystemId(getXmlLabelValue(node, "system-id"));
                    // 本端接口
                    entity.setLocalInterface(getXmlLabelValue(node, "local-interface"));
                    // 邻居状态
                    entity.setNeighborState(getXmlLabelValue(node, "neighbor-state"));
                    // 链路类型
                    entity.setCircuitType(getXmlLabelValue(node, "neighbor-circuit-type"));
                    // 链路标识
                    entity.setCircuitId(getXmlLabelValue(node, "circuit-id"));
                    // 保活时间
                    entity.setHoldTime(getXmlLabelValue(node, "neighbor-holdtime"));
                    // 建立时长
                    entity.setUpTime(getXmlLabelValue(node, "neighbor-uptime"));
                    // 邻居优先级
                    entity.setPriority(getXmlLabelValue(node, "neighbor-priority"));

                    deviceStatusIsisNeighbList.add(entity);
                }
            }
        }
        startPage();
        return getDIYPageTable(deviceStatusIsisNeighbList);
    }

    /**
     * 查询设备状态-isis邻居状态列表
     */
    @ApiOperation(value = "查询设备状态-isis邻居状态列表", httpMethod = "GET")
//    @PreAuthorize("@ss.hasPermi('net:neighb:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceStatusIsisNeighb deviceStatusIsisNeighb) {
        startPage();
        List<DeviceStatusIsisNeighb> list = deviceStatusIsisNeighbService.selectDeviceStatusIsisNeighbList(deviceStatusIsisNeighb);
        return getDataTable(list);
    }

    /**
     * 导出设备状态-isis邻居状态列表
     */
    @ApiOperation(value = "导出设备状态-isis邻居状态列表", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('net:neighb:export')")
    @Log(title = "设备状态-isis邻居状态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceStatusIsisNeighb deviceStatusIsisNeighb) {
        List<DeviceStatusIsisNeighb> list = deviceStatusIsisNeighbService.selectDeviceStatusIsisNeighbList(deviceStatusIsisNeighb);
        ExcelUtil<DeviceStatusIsisNeighb> util = new ExcelUtil<DeviceStatusIsisNeighb>(DeviceStatusIsisNeighb.class);
        util.exportExcel(response, list, "设备状态-isis邻居状态数据");
    }

    /**
     * 获取设备状态-isis邻居状态详细信息
     */
    @ApiOperation(value = "获取设备状态-isis邻居状态详细信息", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('net:neighb:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(deviceStatusIsisNeighbService.selectDeviceStatusIsisNeighbById(id));
    }

    /**
     * 新增设备状态-isis邻居状态
     */
    @ApiOperation(value = "新增设备状态-isis邻居状态", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('net:neighb:add')")
    @Log(title = "设备状态-isis邻居状态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceStatusIsisNeighb deviceStatusIsisNeighb) {
        return toAjax(deviceStatusIsisNeighbService.insertDeviceStatusIsisNeighb(deviceStatusIsisNeighb));
    }

    /**
     * 修改设备状态-isis邻居状态
     */
    @ApiOperation(value = "修改设备状态-isis邻居状态", httpMethod = "PUT")
    //    @PreAuthorize("@ss.hasPermi('net:neighb:edit')")
    @Log(title = "设备状态-isis邻居状态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceStatusIsisNeighb deviceStatusIsisNeighb) {
        return toAjax(deviceStatusIsisNeighbService.updateDeviceStatusIsisNeighb(deviceStatusIsisNeighb));
    }

    /**
     * 删除设备状态-isis邻居状态
     */
    @ApiOperation(value = "删除设备状态-isis邻居状态", httpMethod = "DELETE")
    //    @PreAuthorize("@ss.hasPermi('net:neighb:remove')")
    @Log(title = "设备状态-isis邻居状态", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(deviceStatusIsisNeighbService.deleteDeviceStatusIsisNeighbByIds(ids));
    }
}